package com.panopset.app;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import com.panopset.io.DirectoryProcessor;
import com.panopset.io.Filez;

/**
 * Clean a directory tree.
 *
 * @author Karl Dinwiddie
 *
 */
public class Clean4Bob {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String d = System.getProperty("com.panopset.bob.dir");
        String e = System.getProperty("com.panopset.bob.exts");
        new Clean4Bob().go(d, e);
    }

    private void go(final String dirName, final String exts2save) {
        System.out.println("Cleaning " + dirName);
        final List<String> exts = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(exts2save, ",");
        while (st.hasMoreElements()) {
            exts.add(st.nextToken());
        }
        new DirectoryProcessor(new File(dirName)) {

            @Override
            protected void process(File f) {
                String ext = FilenameUtils.getExtension(f.getName());
                if (StringUtils.isEmpty(ext)) {
                    return;
                }
                for (String x : exts) {
                    if (ext.equals(x)) {
                        return;
                    }
                }
                System.out.println("Attempting to delete " + new Filez().getCanonicalPath(f));
                FileUtils.deleteQuietly(f);
            }
        }.exec();
    }
}
